package com.pets.controller;

import com.pets.entity.Admin;
import com.pets.service.AdminLoginService;
import com.pets.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author
 * 管理员登录控制器
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminLoginController {

    @Autowired
    private AdminLoginService adminLoginService;

    /**
     * 人脸识别登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/faceLogin",method = RequestMethod.POST)
    @ResponseBody
    public Result adminFaceLogin(HttpServletRequest request){
        Result result = new Result();
        Admin  admin = adminLoginService.adminFaceLogin(request);
        if(admin != null){
            request.getSession().setAttribute("admin",admin);
            result.setCode(0);
            result.setMsg("登录成功");
        }else {
            result.setCode(1);
            result.setMsg("登录失败，人脸不存在或符合度低");
        }
        return result;
    }

    /**
     * 管理员人脸录入
     * @param request
     * @return
     */
    @RequestMapping(value = "/adminFaceEntry",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> adminFaceEntry(HttpServletRequest request,String adminId){
        Map result =new HashMap();
        Boolean faceEntryFlag = adminLoginService.adminFaceEntry(request,adminId);
        if(faceEntryFlag){
            result.put("code","200");
            result.put("msg","新增成功");
        }
        return result;
    }

    /**
     * 跳转到密码登录
     * @return
     */
    @RequestMapping(value = "/toPasswordLogin",method = RequestMethod.GET)
    public String toPasswordLogin(){

        return "/admin/login";
    }

    /**
     * 跳转到人脸登录
     * @return
     */
    @RequestMapping(value = "/toFaceLogin",method = RequestMethod.GET)
    public String toFaceLogin(){
       return "/admin/faceLogin";
    }

    @RequestMapping(value = "/toIndex" , method = RequestMethod.GET)
    public String toIndex(){
        return "/admin/index";
    }


    /**
     * 账号密码登录
     * @param admin
     * @param request
     * @return
     */
    @RequestMapping(value = "/doLogin" ,method = RequestMethod.POST)
    @ResponseBody
    public Result doLogin( Admin admin ,HttpServletRequest request){
        Admin admin1 = adminLoginService.doLogin(admin);
        Result result = new Result();
        if(admin1 != null){
            request.getSession().setAttribute("admin",admin);
            result.setCode(0);
            result.setMsg("登录成功");
        }else {
            result.setCode(1);
            result.setMsg("账号或者密码错误");
        }
        return result;
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "/exit" , method = RequestMethod.GET)
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("admin");
        return "/admin/login";
    }
}
